﻿using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string text = "<name><first>Ivo</first></name><age>29</age>";
        string tagPattern = @"\<(?<tag>\w+)\>";
        MatchCollection tags = Regex.Matches(text, tagPattern);
        foreach (Match tag in tags)
        {
            Match tagName = Regex.Match(tag.Value, @"\w+");
            string tagStartPattern = @"<" + Regex.Escape(tagName.Value) + @">";
            string tagEndPattern = @"</" + Regex.Escape(tagName.Value) + @">";
            string tagFinalPattern = Regex.Escape(tagStartPattern) + @"(?<value>(.|\s)+)" + Regex.Escape(tagEndPattern);
            Match tagValue = Regex.Match(text, tagFinalPattern);
            Group gr = tagValue.Groups["value"];
            Console.WriteLine("tag={0}, value={1}",tagName,gr.Value);
        }        
    }
}
